package com.csu.ui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import com.csu.dao.MemberDao;
import com.csu.dao.MemberDaoImpl;

public class UserSecPanel extends JPanel {

	//修改密码 -- 初始密码
	JLabel orgPwdLabel = new JLabel("初始密码：");
	JPasswordField orgPwdField = new JPasswordField();
	//修改密码 -- 新密码
	JLabel newPwdLabel = new JLabel("新密码：");
	JPasswordField newPwdField = new JPasswordField();
	//修改密码 -- 确认密码
	JLabel confirmPwdLabel = new JLabel("确认密码：");
	JPasswordField confirmPwdField = new JPasswordField();
	//查询用户 -- 表格
	TablePanel tablePanel = new TablePanel();
	//查询用户 -- 用户名
	JLabel selectUserLabel = new JLabel("用户名：");
	JTextField selectUserField = new JTextField();
	//查询用户 -- 用户类型
	JLabel selectTypeLabel = new JLabel("用户类型：");
	JComboBox<String> selectTypeBox = new JComboBox<String>();
	//查询用户 -- 用户名查询按钮
	JButton selectAcctBtn = new JButton("按用户名查询");
	//查询用户 -- 用户类型查询按钮
	JButton selectTypeBtn = new JButton("按类型查询");
	//查询用户 -- 返回按钮
	JButton backBtn = new JButton("返回");
	//添加用户 -- 用户名
	JLabel acctLabel = new JLabel("用户名：");
	JTextField acctField = new JTextField();
	//添加用户 -- 密码
	JLabel pwdLabel = new JLabel("密码：");
	JPasswordField pwdField = new JPasswordField();
	//添加用户 -- 确认密码
	JLabel secPwdLabel = new JLabel("确认密码：");
	JPasswordField secPwdField = new JPasswordField();
	//添加用户 -- 用户类型
	JLabel typeLabel = new JLabel("用户类型：");
	JComboBox<String> typeBox = new JComboBox<String>();
	//删除用户 -- 用户名
	JLabel delLabel = new JLabel("用户名：");
	JTextField delField = new JTextField();
	//更改用户 -- 初始用户名
	JLabel orgAcctLabel = new JLabel("初始用户名：");
	JTextField orgAcctField = new JTextField();
	//更改用户 -- 新用户名
	JLabel newAcctLabel = new JLabel("新用户名：");
	JTextField newAcctField = new JTextField();
	//更改用户 -- 用户类型
	JLabel newTypeLabel = new JLabel("用户类型：");
	JComboBox<String> newTypeBox = new JComboBox<String>();

	
	//用户名
	private String currAcct;
	
	public String getCurrAcct() {
		return currAcct;
	}

	public void setCurrAcct(String currAcct) {
		this.currAcct = currAcct;
	}

	private JPanel helpPanel = new JPanel();
	private JPanel changePwdPanel = new JPanel();
	private JPanel selectPanel = new JPanel();
	private JPanel addPanel = new JPanel();
	private JPanel deletePanel = new JPanel();
	private JPanel changePanel = new JPanel();
	
	public void backBtnTrue() {
		backBtn.setEnabled(true);
	}
	
	public void backBtnFalse() {
		backBtn.setEnabled(false);
	}
	
	public void changePwdPanelTrue() {
		helpPanel.setVisible(false);
		changePwdPanel.setVisible(true);
		selectPanel.setVisible(false);
		addPanel.setVisible(false);
		deletePanel.setVisible(false);
		changePanel.setVisible(false);
		orgPwdField.requestFocus();
	}
	public void selectPanelTrue() {
		helpPanel.setVisible(false);
		changePwdPanel.setVisible(false);
		selectPanel.setVisible(true);
		addPanel.setVisible(false);
		deletePanel.setVisible(false);
		changePanel.setVisible(false);
		selectUserField.requestFocus();
	}
	public void addPanelTrue() {
		helpPanel.setVisible(false);
		changePwdPanel.setVisible(false);
		selectPanel.setVisible(false);
		addPanel.setVisible(true);
		deletePanel.setVisible(false);
		changePanel.setVisible(false);
		acctField.requestFocus();
	}
	public void deletePanelTrue() {
		helpPanel.setVisible(false);
		changePwdPanel.setVisible(false);
		selectPanel.setVisible(false);
		addPanel.setVisible(false);
		deletePanel.setVisible(true);
		changePanel.setVisible(false);
		delField.requestFocus();
	}
	public void changePanelTrue() {
		helpPanel.setVisible(false);
		changePwdPanel.setVisible(false);
		selectPanel.setVisible(false);
		addPanel.setVisible(false);
		deletePanel.setVisible(false);
		changePanel.setVisible(true);
		orgAcctField.requestFocus();
	}
	
	public void allFalse() {
		helpPanel.setVisible(true);
		changePwdPanel.setVisible(false);
		selectPanel.setVisible(false);
		addPanel.setVisible(false);
		deletePanel.setVisible(false);
		changePanel.setVisible(false);
	}
	
	UserSecPanel(){
		this.setBackground(Color.WHITE);
		this.setLayout(null);

		/***** 帮助 *****/
		helpPanel.setBounds(25, 25, 750, 575);
		helpPanel.setBackground(Color.WHITE);
		JLabel helpLabel = new JLabel("<html><h1>帮助</h1>\n" +
				"<h2>修改密码</h2>\n" +
				"<p>\n" +
				"<u>所有人</u>可用，通过输入初始密码、新密码并确认新密码可以修改自己的密码。<br>\n" +
				"<i>*初始密码必须正确</i><br>\n" +
				"<i>*新密码必须与确认密码一致</i>\n" +
				"</p>\n" +
				"<h2>查询用户</h2>\n" +
				"<p>\n" +
				"<u>管理员</u>可用，点击可以查看所有用户，也可以通过输入用户名查看某一用户或通过用户类型查看某一类用户。\n" +
				"</p>\n" +
				"<h2>添加用户</h2>\n" +
				"<p>\n" +
				"<u>管理员</u>可用，通过输入用户名、密码并确认密码、选择用户类型可以添加新用户。<br>\n" +
				"<i>*用户名必须有效</i><br>\n" +
				"<i>*密码必须与确认密码一致</i>\n" +
				"</p>\n" +
				"<h2>删除用户</h2>\n" +
				"<p>\n" +
				"<u>管理员</u>可用，通过输入用户名可以删除某一用户。<br>\n" +
				"<i>*用户名必须存在</i>\n" +
				"</p>\n" +
				"<h2>更改用户</h2>\n" +
				"<p>\n" +
				"<u>管理员</u>可用，通过输入原用户名和新用户名、选择用户类型可以更改某一用户的信息。<br>\n" +
				"<i>*初始用户名必须存在</i><br>\n" +
				"<i>*新用户名必须有效</i>\n" +
				"</p></html>");
		helpPanel.add(helpLabel);
		this.add(helpPanel);
		helpPanel.setVisible(false);

		/***** 修改密码 *****/
		changePwdPanel.setBounds(25, 25, 750, 575);
		changePwdPanel.setBackground(Color.WHITE);
		//初始密码
		JPanel panel11 = new JPanel();
		panel11.setBackground(Color.WHITE);
		orgPwdLabel.setBounds(0, 0, 75, 50);
		panel11.add(orgPwdLabel);
		orgPwdField.setBounds(75, 10, 250, 30);
		panel11.add(orgPwdField);
		panel11.setLayout(null);
		//新密码
		JPanel panel12 = new JPanel();
		panel12.setBackground(Color.WHITE);
		newPwdLabel.setBounds(0, 0, 75, 50);
		panel12.add(newPwdLabel);
		newPwdField.setBounds(75, 10, 250, 30);
		panel12.add(newPwdField);
		panel12.setLayout(null);
		//确认密码
		JPanel panel14 = new JPanel();
		panel14.setBackground(Color.WHITE);
		confirmPwdLabel.setBounds(0, 0, 75, 50);
		panel14.add(confirmPwdLabel);
		confirmPwdField.setBounds(75, 10, 250, 30);
		panel14.add(confirmPwdField);
		panel14.setLayout(null);
		//确认修改按钮
		JPanel panel13 = new JPanel();
		panel13.setBackground(Color.WHITE);
		JButton confirmBtn = new JButton("确认修改");
		confirmBtn.setBounds(100,0,100,30);
		panel13.add(confirmBtn);
		panel13.setLayout(null);
		//按钮事件
		confirmBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MemberDao memberDao = new MemberDaoImpl();
				if(String.valueOf(orgPwdField.getPassword()).equals("") || String.valueOf(newPwdField.getPassword()).equals("") || String.valueOf(confirmPwdField.getPassword()).equals("")) {
				}
				else {
					if(memberDao.findPwd(currAcct, String.valueOf(orgPwdField.getPassword()))==1 && String.valueOf(newPwdField.getPassword()).equals(String.valueOf(confirmPwdField.getPassword()))) {
						memberDao.updatePwd(currAcct, String.valueOf(newPwdField.getPassword()));
						JOptionPane.showMessageDialog(null,"修改成功","提示",JOptionPane.INFORMATION_MESSAGE);
						orgPwdField.setText("");
						newPwdField.setText("");
						confirmPwdField.setText("");
						orgPwdField.requestFocus();
					}
					else {
						JOptionPane.showMessageDialog(null,
								"修改失败，可能由于以下原因导致："+'\n'+'\n'+
										"-原始密码错误"+'\n'+
										"-两次密码输入不一致",
								"提示",JOptionPane.ERROR_MESSAGE);
						orgPwdField.setText("");
						newPwdField.setText("");
						confirmPwdField.setText("");
						orgPwdField.requestFocus();
					}
				}
			}
		});
		//添加组件
		changePwdPanel.setLayout(null);
		panel11.setBounds(0, 0, 750, 50);
		changePwdPanel.add(panel11);
		panel12.setBounds(0, 75, 750, 50);
		changePwdPanel.add(panel12);
		panel14.setBounds(0, 150, 750, 50);
		changePwdPanel.add(panel14);
		panel13.setBounds(0, 225, 750, 50);
		changePwdPanel.add(panel13);
		this.add(changePwdPanel);
		changePwdPanel.setVisible(false);
		
		/***** 查询用户 *****/
		selectPanel.setBounds(25, 25, 750, 575);
		selectPanel.setBackground(Color.WHITE);
		//表格
		tablePanel.initData();
		tablePanel.initPage();
		//用户名用户类型
		JPanel panel21 = new JPanel();
		panel21.setBackground(Color.WHITE);
		selectUserLabel.setBounds(0, 0, 75, 50);
		panel21.add(selectUserLabel);
		selectUserField.setBounds(75, 10, 200, 30);
		panel21.add(selectUserField);
		selectTypeLabel.setBounds(350, 0, 75, 50);
		panel21.add(selectTypeLabel);
		selectTypeBox.addItem("管理员");
		selectTypeBox.addItem("普通用户");
		selectTypeBox.setSelectedItem("普通用户");
		selectTypeBox.setBounds(425, 10, 100, 30);
		panel21.add(selectTypeBox);
		panel21.setLayout(null);
		//按钮
		JPanel panel22 = new JPanel();
		panel22.setBackground(Color.WHITE);
		selectAcctBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(selectUserField.getText().equals("")) {
				}
				else {
					selectAcctBtn.setEnabled(false);
					selectTypeBtn.setEnabled(false);
					backBtnTrue();
					tablePanel.initData(selectUserField.getText());
					tablePanel.setCurrPage(1);
					tablePanel.setLabel2();
					tablePanel.setLabel3();
					tablePanel.setBtn();
				}
			}
		});
		selectAcctBtn.setBounds(100,0,125,30);
		panel22.add(selectAcctBtn);
		selectTypeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				selectAcctBtn.setEnabled(false);
				selectTypeBtn.setEnabled(false);
				backBtnTrue();
				tablePanel.initData(selectTypeBox.getSelectedIndex());
				tablePanel.setCurrPage(1);
				tablePanel.setLabel2();
				tablePanel.setLabel3();
				tablePanel.setBtn();
			}
		});
		selectTypeBtn.setBounds(275,0,125,30);
		panel22.add(selectTypeBtn);
		backBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				selectTypeBtn.setEnabled(true);
				selectAcctBtn.setEnabled(true);
				backBtnFalse();
				selectUserField.setText("");
				selectUserField.requestFocus();
				selectTypeBox.setSelectedItem("普通用户");
				tablePanel.initData();
				tablePanel.setCurrPage(1);
				tablePanel.setLabel2();
				tablePanel.setLabel3();
				tablePanel.setBtn();
			}
		});
		backBtn.setEnabled(false);
		backBtn.setBounds(450,0,125,30);
		panel22.add(backBtn);
		panel22.setLayout(null);
		//添加组件
		selectPanel.setLayout(null);
		tablePanel.setBounds(50, 150, 650, 400);
		selectPanel.add(tablePanel);
		panel21.setBounds(0, 0, 750 ,50);
		selectPanel.add(panel21);
		panel22.setBounds(0, 75, 750 ,50);
		selectPanel.add(panel22);
		this.add(selectPanel);
		selectPanel.setVisible(false);
		
		/***** 添加用户 *****/
		addPanel.setBounds(25, 25, 750, 575);
		addPanel.setBackground(Color.WHITE);
		//用户名
		JPanel panel31 = new JPanel();
		panel31.setBackground(Color.WHITE);
		acctLabel.setBounds(0, 0, 75, 50);
		panel31.add(acctLabel);
		acctField.setBounds(75, 10, 250, 30);
		panel31.add(acctField);
		panel31.setLayout(null);
		//密码
		JPanel panel32 = new JPanel();
		panel32.setBackground(Color.WHITE);
		pwdLabel.setBounds(0, 0, 75, 50);
		panel32.add(pwdLabel);
		pwdField.setBounds(75, 10, 250, 30);
		panel32.add(pwdField);
		panel32.setLayout(null);
		//确认密码
		JPanel panel35 = new JPanel();
		panel35.setBackground(Color.WHITE);
		secPwdLabel.setBounds(0, 0, 75, 50);
		panel35.add(secPwdLabel);
		secPwdField.setBounds(75, 10, 250, 30);
		panel35.add(secPwdField);
		panel35.setLayout(null);
		//类型
		JPanel panel33 = new JPanel();
		panel33.setBackground(Color.WHITE);
		typeBox.addItem("管理员");
		typeBox.addItem("普通用户");
		typeBox.setSelectedItem("普通用户");
		typeLabel.setBounds(0, 0, 75, 50);
		panel33.add(typeLabel);
		typeBox.setBounds(75, 10, 250, 30);
		panel33.add(typeBox);
		panel33.setLayout(null);
		//添加按钮
		JPanel panel34 = new JPanel();
		panel34.setBackground(Color.WHITE);
		JButton addBtn = new JButton("添加");
		addBtn.setBounds(100,0,100,30);
		panel34.add(addBtn);
		panel34.setLayout(null);
		//按钮事件
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MemberDao memberDao = new MemberDaoImpl();
				if(acctField.getText().equals("") || String.valueOf(pwdField.getPassword()).equals("") || String.valueOf(secPwdField.getPassword()).equals("")) {
				}
				else {
					if(memberDao.findUser(acctField.getText())==1 || !String.valueOf(pwdField.getPassword()).equals(String.valueOf(secPwdField.getPassword()))) {
						JOptionPane.showMessageDialog(null,
								"添加失败，可能由于以下原因导致："+'\n'+'\n'+
								"-用户名已存在"+'\n'+
								"-两次密码输入不一致",
								"提示",JOptionPane.ERROR_MESSAGE);
						acctField.setText("");
						pwdField.setText("");
						secPwdField.setText("");
						typeBox.setSelectedItem("普通用户");
						acctField.requestFocus();
					}
					else {
						memberDao.insertUser(typeBox.getSelectedIndex(),acctField.getText(),String.valueOf(pwdField.getPassword()));
						JOptionPane.showMessageDialog(null,"添加成功","提示",JOptionPane.INFORMATION_MESSAGE);
						acctField.setText("");
						pwdField.setText("");
						secPwdField.setText("");
						typeBox.setSelectedItem("普通用户");
						acctField.requestFocus();
					}
				}
			}
		});
		//添加组件
		addPanel.setLayout(null);
		panel31.setBounds(0, 0, 750, 50);
		addPanel.add(panel31);
		panel32.setBounds(0, 75, 750, 50);
		addPanel.add(panel32);
		panel35.setBounds(0, 150, 750, 50);
		addPanel.add(panel35);
		panel33.setBounds(0, 225, 750, 50);
		addPanel.add(panel33);
		panel34.setBounds(0, 300, 750, 50);
		addPanel.add(panel34);
		this.add(addPanel);
		addPanel.setVisible(false);
		
		/***** 删除用户 *****/
		deletePanel.setBounds(25, 25, 750, 575);
		deletePanel.setBackground(Color.WHITE);
		//用户名
		JPanel panel41 = new JPanel();
		panel41.setBackground(Color.WHITE);
		delLabel.setBounds(0, 0, 75, 50);
		panel41.add(delLabel);
		delField.setBounds(75, 10, 250, 30);
		panel41.add(delField);
		panel41.setLayout(null);
		//删除按钮
		JPanel panel42 = new JPanel();
		panel42.setBackground(Color.WHITE);
		JButton deleteBtn = new JButton("删除");
		deleteBtn.setBounds(100,0,100,30);
		panel42.add(deleteBtn);
		panel42.setLayout(null);
		//按钮事件
		deleteBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MemberDao memberDao = new MemberDaoImpl();
				if(!delField.getText().equals("")) {
					if(JOptionPane.showConfirmDialog(null,"是否删除","提示",JOptionPane.YES_NO_OPTION)==0) {
						if(memberDao.findUser(delField.getText())==1) {
							if(memberDao.deleteUser(delField.getText())==1)
								JOptionPane.showMessageDialog(null,"删除成功","提示",JOptionPane.INFORMATION_MESSAGE);
							else
								JOptionPane.showMessageDialog(null,
										"删除失败，可能由于以下原因导致："+'\n'+'\n'+
										"-用户名不存在",
										"提示",JOptionPane.ERROR_MESSAGE);
						}
						else {
							JOptionPane.showMessageDialog(null,
									"删除失败，可能由于以下原因导致："+'\n'+'\n'+
											"-用户名不存在",
									"提示",JOptionPane.ERROR_MESSAGE);
						}
					}
					delField.setText("");
					delField.requestFocus();
				}
			}
		});
		//添加组件
		deletePanel.setLayout(null);
		panel41.setBounds(0, 0, 750, 50);
		deletePanel.add(panel41);
		panel42.setBounds(0, 75, 750, 50);
		deletePanel.add(panel42);
		this.add(deletePanel);
		deletePanel.setVisible(false);
		
		/***** 更改用户 *****/
		changePanel.setBounds(25, 25, 750, 575);
		changePanel.setBackground(Color.WHITE);
		//初始用户名
		JPanel panel51 = new JPanel();
		panel51.setBackground(Color.WHITE);
		orgAcctLabel.setBounds(0, 0, 75, 50);
		panel51.add(orgAcctLabel);
		orgAcctField.setBounds(75, 10, 250, 30);
		panel51.add(orgAcctField);
		panel51.setLayout(null);
		//新用户名
		JPanel panel52 = new JPanel();
		panel52.setBackground(Color.WHITE);
		newAcctLabel.setBounds(0, 0, 75, 50);
		panel52.add(newAcctLabel);
		newAcctField.setBounds(75, 10, 250, 30);
		panel52.add(newAcctField);
		panel52.setLayout(null);
		//类型
		JPanel panel53 = new JPanel();
		panel53.setBackground(Color.WHITE);
		newTypeBox.addItem("管理员");
		newTypeBox.addItem("普通用户");
		newTypeBox.setSelectedItem("普通用户");
		newTypeLabel.setBounds(0, 0, 75, 50);
		panel53.add(newTypeLabel);
		newTypeBox.setBounds(75, 10, 250, 30);
		panel53.add(newTypeBox);
		panel53.setLayout(null);
		//修改按钮
		JPanel panel54 = new JPanel();
		panel54.setBackground(Color.WHITE);
		JButton changeBtn = new JButton("确认修改");
		changeBtn.setBounds(100,0,100,30);
		panel54.add(changeBtn);
		panel54.setLayout(null);
		//按钮事件
		changeBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				MemberDao memberDao = new MemberDaoImpl();
				if(orgAcctField.getText().equals("") || newAcctField.getText().equals("")) {
				}
				else {
					if(memberDao.changeUser(orgAcctField.getText(),newAcctField.getText(),newTypeBox.getSelectedIndex())==1) {
						JOptionPane.showMessageDialog(null,"修改成功","提示",JOptionPane.INFORMATION_MESSAGE);
						orgAcctField.setText("");
						newAcctField.setText("");
						newTypeBox.setSelectedItem("普通用户");
						orgAcctField.requestFocus();
					}
					else {
						JOptionPane.showMessageDialog(null,
								"修改失败，可能由于以下原因导致："+'\n'+'\n'+
										"-原始用户名不存在"+'\n'+
										"-新用户名已存在",
								"提示",JOptionPane.ERROR_MESSAGE);
						orgAcctField.setText("");
						newAcctField.setText("");
						newTypeBox.setSelectedItem("普通用户");
						orgAcctField.requestFocus();
					}
				}
				
			}
		});
		//添加组件
		changePanel.setLayout(null);
		panel51.setBounds(0, 0, 750, 50);
		changePanel.add(panel51);
		panel52.setBounds(0, 75, 750, 50);
		changePanel.add(panel52);
		panel53.setBounds(0, 150, 750, 50);
		changePanel.add(panel53);
		panel54.setBounds(0, 225, 750, 50);
		changePanel.add(panel54);
		this.add(changePanel);
		changePanel.setVisible(false);
		
	}
	
}
